home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / contextlib.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  4KB  |  165 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. '''Utilities for with-statement contexts.  See PEP 343.'''
  5. import sys
  6. from functools import wraps
  7. from warnings import warn
  8. __all__ = [
  9.     'contextmanager',
  10.     'nested',
  11.     'closing']
  12.  
  13. class GeneratorContextManager(object):
  14.     '''Helper for @contextmanager decorator.'''
  15.     
  16.     def __init__(self, gen):
  17.         self.gen = gen
  18.  
  19.     
  20.     def __enter__(self):
  21.         
  22.         try:
  23.             return self.gen.next()
  24.         except StopIteration:
  25.             raise RuntimeError("generator didn't yield")
  26.  
  27.  
  28.     
  29.     def __exit__(self, type, value, traceback):
  30.         if type is None:
  31.             
  32.             try:
  33.                 self.gen.next()
  34.             except StopIteration:
  35.                 return None
  36.  
  37.             raise RuntimeError("generator didn't stop")
  38.         if value is None:
  39.             value = type()
  40.         
  41.         try:
  42.             self.gen.throw(type, value, traceback)
  43.             raise RuntimeError("generator didn't stop after throw()")
  44.         except StopIteration:
  45.             exc = None
  46.             return exc is not value
  47.             if sys.exc_info()[1] is not value:
  48.                 raise 
  49.  
  50.  
  51.  
  52.  
  53. def contextmanager(func):
  54.     '''@contextmanager decorator.
  55.  
  56.     Typical usage:
  57.  
  58.         @contextmanager
  59.         def some_generator(<arguments>):
  60.             <setup>
  61.             try:
  62.                 yield <value>
  63.             finally:
  64.                 <cleanup>
  65.  
  66.     This makes this:
  67.  
  68.         with some_generator(<arguments>) as <variable>:
  69.             <body>
  70.  
  71.     equivalent to this:
  72.  
  73.         <setup>
  74.         try:
  75.             <variable> = <value>
  76.             <body>
  77.         finally:
  78.             <cleanup>
  79.  
  80.     '''
  81.     
  82.     def helper(*args, **kwds):
  83.         return GeneratorContextManager(func(*args, **kwds))
  84.  
  85.     helper = (wraps(func),)(helper)
  86.     return helper
  87.  
  88.  
  89. def nested(*managers):
  90.     '''Combine multiple context managers into a single nested context manager.
  91.  
  92.    This function has been deprecated in favour of the multiple manager form
  93.    of the with statement.
  94.  
  95.    The one advantage of this function over the multiple manager form of the
  96.    with statement is that argument unpacking allows it to be
  97.    used with a variable number of context managers as follows:
  98.  
  99.       with nested(*managers):
  100.           do_something()
  101.  
  102.     '''
  103.     warn('With-statements now directly support multiple context managers', DeprecationWarning, 3)
  104.     exits = []
  105.     vars = []
  106.     exc = (None, None, None)
  107.     
  108.     try:
  109.         for mgr in managers:
  110.             exit = mgr.__exit__
  111.             enter = mgr.__enter__
  112.             vars.append(enter())
  113.             exits.append(exit)
  114.         
  115.         yield vars
  116.     except:
  117.         exc = sys.exc_info()
  118.     finally:
  119.         while exits:
  120.             exit = exits.pop()
  121.             
  122.             try:
  123.                 if exit(*exc):
  124.                     exc = (None, None, None)
  125.             continue
  126.             exc = sys.exc_info()
  127.             continue
  128.  
  129.         if exc != (None, None, None):
  130.             raise exc[0], exc[1], exc[2]
  131.  
  132.  
  133. nested = contextmanager(nested)
  134.  
  135. class closing(object):
  136.     '''Context to automatically close something at the end of a block.
  137.  
  138.     Code like this:
  139.  
  140.         with closing(<module>.open(<arguments>)) as f:
  141.             <block>
  142.  
  143.     is equivalent to this:
  144.  
  145.         f = <module>.open(<arguments>)
  146.         try:
  147.             <block>
  148.         finally:
  149.             f.close()
  150.  
  151.     '''
  152.     
  153.     def __init__(self, thing):
  154.         self.thing = thing
  155.  
  156.     
  157.     def __enter__(self):
  158.         return self.thing
  159.  
  160.     
  161.     def __exit__(self, *exc_info):
  162.         self.thing.close()
  163.  
  164.  
  165.